import { ReplayActionType } from '@act/replayAction'
import cloneDeep from 'lodash/cloneDeep'
import { Type, Data, singleClone, setZero } from '@utils/Global'

const _state = {
  showType: {
    startBtn: true,
    endBtn: false,
    toQaBtn: false,
    footer: false,
    mediaLoading: false,
    videoMask: false,
    qaScreen: false,
    mediaError: false,
    dm: false
  },
  noAudio: {
    show: true,
    loading: false
  },
  swiper: {
    images: [],
    index: 0
  },
  marks: [],
  dmList: [],
  qa: {
    list: [],
    big_image_url: '',
    voiceStatus: {}
  },
  footer: {
    currentTime: 0,
    fullTime: 0,
    isPause: true
  }
}
const ReplayState = (state = _state, action) => {
  switch (action.type) {
    case ReplayActionType.reset_all_state: {
      return _state
    }
    case ReplayActionType.set_replay_marks: {
      const s = singleClone(state)
      s.marks = action.payload.marks.map((item, index) => {
        const second = Number(item.second)
        const minute = Number(item.minute)
        const time = `${setZero(minute)}:${setZero(second)}`
        return Object.assign({}, item, {
          title: item.title,
          time,
          second,
          minute,
          diff: minute * 60 + second,
          active: index === 0,
          index: index
        })
      })
      console.log(s.marks)
      return s
    }
    case ReplayActionType.update_no_audio: {
      const s = cloneDeep(state)
      const { type, value } = action.payload
      s.noAudio[type] = value
      return s
    }
    case ReplayActionType.toggle_show_type: {
      const s = cloneDeep(state)
      const { type, show } = action.payload
      s.showType[type] = show
      return s
    }
    case ReplayActionType.init_swiper: {
      const s = cloneDeep(state)
      const { images } = action.payload
      s.swiper.images = images
      return s
    }
    case ReplayActionType.change_swiper_index: {
      const s = cloneDeep(state)
      const { index } = action.payload
      s.swiper.index = index
      return s
    }
    case ReplayActionType.init_qa_list: {
      const s = cloneDeep(state)
      const { list } = action.payload
      s.qa.list = list
      return s
    }
    case ReplayActionType.stop_all_voice:
      const s = cloneDeep(state)
      s.qa.list.map(r => {
        if (r.type === Type.COMMON_TYPE.COMMON_QA_VOICE) {
          r.voiceStatus.playing = false
          r.voiceStatus.loading = false
        }
        if (r.type === Type.COMMON_TYPE.COMMON_QA_ANSWER
          && r.data.answer.data.type === 1) {
          r.voiceStatus.playing = false
          r.voiceStatus.loading = false
        }
      })
      return s
    case ReplayActionType.update_voice_status: {
      const s = cloneDeep(state)
      const { status, url, type: voiceType } = action.payload
      if (voiceType === 'voice') {
        var current = s.qa.list.filter((_) => {
          return _.type === Type.COMMON_TYPE.COMMON_QA_VOICE && _.data.file === url
        })
        if (current.length > 0) {
          current[0].voiceStatus = status
        }
      } else if (voiceType === 'answer') {
        let ac = s.qa.list.find(r => {
          return r.type === Type.COMMON_TYPE.COMMON_QA_ANSWER
            && r.data.answer.data.type === 1
            && r.data.answer.data.file === url
        })
        if (ac) {
          ac.voiceStatus = status
        }
      }

      return s
    }
    case ReplayActionType.toggle_big_image: {
      const s = cloneDeep(state)
      const { image } = action.payload
      s.qa.big_image_url = image
      return s
    }
    case ReplayActionType.update_footer: {
      const s = cloneDeep(state)
      const { type, value } = action.payload
      s.footer[type] = value
      return s
    }
    case ReplayActionType.set_dm_list: {
      const s = cloneDeep(state)
      const { list } = action.payload
      s.dmList = list
      return s
    }
    case ReplayActionType.do_self_like_item: {
      console.log('333', action.payload)
      const s = cloneDeep(state)
      let dsli = null
      if (action.payload.msgType === 'question') {
        dsli = s.qa.list.find(r => r.type === Type.COMMON_TYPE.COMMON_QA_QUESTION && r.data.question.id === action.payload.id)
        if (dsli) {
          dsli.data.question.up_num += 1
          dsli.data.question.like = true
          dsli.data.question.users.push({
            id: Data.user.id,
            name: Data.user.nickname,
            headimgurl: Data.user.headimgurl
          })
        }
      }
      if (action.payload.msgType === 'answer') {
        dsli = s.qa.list.find(r => r.type === Type.COMMON_TYPE.COMMON_QA_ANSWER && r.data.answer.id === action.payload.id)
        if (dsli) {
          dsli.data.answer.up_num += 1
          dsli.data.answer.like = true
          dsli.data.answer.users.push({
            id: Data.user.id,
            name: Data.user.nickname,
            headimgurl: Data.user.headimgurl
          })
        }
      }
      return s
    }
    default:
      return state
  }
}
ReplayState._default_state = _state
export default ReplayState
